package command;

import java.net.InetAddress;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.*;
import javax.servlet.http.*;

import command.Command;
import command.CommandException;
import db.CommonConnect;

import common.CommonUtil;
import common.Crypto;
import common.EDUBOX_Baese64_Crypto;
import common.UserSession;
import db.QueryManager;
import db.DAO;

/**
 * Command 인터페이스를 implement 하여 구현된 command.
 */
public class PreptestCommand extends Command {

	private String next;
	
	public PreptestCommand(String next) {
		this.next=next;
	}

	/**
	 * 실제 요청을 받아 처리하는 메소드
	 * 요청을 받으면  요청을 처리하고 next 페이지를 돌려줌
	 * 
	 * @param req HttpServletRequest
	 * @throws CommandException
	 * @return next 이동할 페이지
	 */
	public String execute(HttpServletRequest req)
	throws CommandException {
		ArrayList list =  null;
		ArrayList list_1 =  null;
		HashMap listMap = new HashMap();
		
		Connection con = null;  
		try {
			HttpSession session = req.getSession();
			UserSession userSession = (UserSession)session.getAttribute("user"); 

			String login_id = null;
			String user_id = null;
			String level = null;
			
			user_id = CommonUtil.nvl(req.getParameter("user_id"));

			if(userSession!= null){
				login_id = userSession.getLogin_id();
				if(user_id.equals("")){
					user_id = userSession.getUser_id();
				}
				level = userSession.getAdmin_level();
			}
			
			System.out.println("user_id====>" + user_id);
			System.out.println("level====>" + level);
			String rating_state = CommonUtil.nvl(req.getParameter("rating_state"));
			String search_combo = CommonUtil.nvl(req.getParameter("search_combo"));
			String keyword = CommonUtil.nvl(req.getParameter("keyword"));
			String line_number = CommonUtil.nvl(req.getParameter("line_number"),"10");
			String total_count = CommonUtil.nvl(req.getParameter("total_count"),"0");
			String curr_page = CommonUtil.nvl(req.getParameter("curr_page"),"1");
			
			String start_date = CommonUtil.nvl(req.getParameter("start_date"));
			String end_date = CommonUtil.nvl(req.getParameter("end_date"));
			
			String s_test_gubun = CommonUtil.nvl(req.getParameter("s_test_gubun"));
			String s_member_dist = CommonUtil.nvl(req.getParameter("s_member_dist"));
			String s_rater = CommonUtil.nvl(req.getParameter("s_rater"));
			
			if(search_combo.equals("2")){
				keyword = CommonUtil.nvl(Crypto.EncodeBySType(keyword));
				System.out.println("keyword====>" + keyword);
			}
						
			con = (new CommonConnect()).getConnection_ds1();
			con.setAutoCommit(false);
			
			String[] param=null;
			String queryStr = "call sp_sp1201_list(?,?,?,?,?,?,?,?,?,?,?,?,?)";
			
			param= new String[13];
			param[0] = user_id;	
			param[1] = level;	
			param[2] = search_combo;	
			param[3] = keyword;	
			param[4] = line_number;	
			param[5] = total_count;	
			param[6] = curr_page;
			param[7] = rating_state;
			
			param[8] = start_date;
			param[9] = end_date;
			
			param[10] = s_test_gubun;
			param[11] = s_member_dist;
			param[12] = s_rater;
			
			list = DAO.select(con,queryStr,param);
			
			listMap.put("mainContent",list);
			logger.debug("queryStr ==> "+"call sp_sp1201_list(?,?,?,?,?,?,?,?,?,?)");	
			
			
			param=null;
			queryStr = QueryManager.getQuery("ajax_common_code.query");
			
			param= new String[1];
			param[0] = "rater";	
			
			list_1 = DAO.select(con,queryStr,param);
			
			listMap.put("mainContent_1",list_1);
			logger.debug("queryStr ==> "+"ajax_common_code.query");	
			
		     
// 리퀘스트에 모두 담는다.
			req.setAttribute("list_map",listMap);

	

		}catch (Exception ex) {
			try{
				con.rollback();
			}catch(Exception ee){}
			ex.printStackTrace();
			throw new CommandException(ex.getMessage());
		}finally {
			try{
				if (con!=null) con.setAutoCommit(true);
				if (con!=null) con.close();
			}catch(Exception ee){}
		}
		return next;
	}
	


}
